package com.springmvc.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class RequestScopeTestController {

    @RequestMapping("/testRequestScope1")
    public String testRequestApi1(HttpServletRequest  request)
    {
        //将共享数据存到request域当中
        request.setAttribute("testRequestScope", "在spring当中使用原生servlet API完成request域共享");
        //跳转视图，在视图页面将request当中的数据取出，这样就完成了：controller和view组件之间的数据共享
        //这个跳转，默认请境况下是转发的方式
        return "Ok";
    }
    @RequestMapping("/testRequestScope2")
    public String testRequestApi2(Model  model){
        //将共享数据存入request域当中
        model.addAttribute("testRequestScope", "在spring当中使用Model完成request域共享");
        System.out.println(model.getClass().getName());
        return "Ok";
    }
    @RequestMapping("/testRequestScope3")
    public String testRequestApi3(Map<String,Object> map){
        //将共享数据存入request域当中
        map.put("testRequestScope", "在spring当中使用Map完成request域共享");
        System.out.println(map.getClass().getName());
        return "Ok";
    }
    @RequestMapping("/testRequestScope4")
    public String testRequestApi4(ModelMap  model){
        //将共享数据存入request域当中
        model.addAttribute("testRequestScope", "在spring当中使用ModelMap完成request域共享");
        System.out.println(model.getClass().getName());
        return "Ok";
    }
}
